let form = layui.form;
form.verify({
    nickname: val => {
        if (val.length > 6) return '昵称过长,不得超过6个字符'
    }
});

function initInfo() {
    $.ajax({
        type: "GET",
        url: '/my/userinfo',
        success: res => {
            const {
                status,
                message,
                data
            } = res;
            if (status !== 0) return layui.layer.msg(message);
            form.val("formUserInfo", data)
        }
    })
};
initInfo();
//重置按钮
$(".btn-reset").on("click", function (e) {
    e.preventDefault()
    initInfo();
});
//提交修改
// 表单注册submit提交事件,阻止默认行为,添加input隐藏域得到id属性值,得到表单数据,发送ajax中post请求,判断状态值,成功后用window.parent调用父页面中渲染头像函数. 
$(".layui-form").submit(function (e) {
    e.preventDefault()
    let data = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "/my/userinfo",
        data,
        success: res => {
            const { status, message } = res;
            if (status !== 0) return layui.layer.msg(message);
            window.parent.getUser();
        }
    })
})